/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.envconfig;

import com.google.inject.Inject;
import com.google.inject.Provider;
import javax.sql.DataSource;
import javax.persistence.EntityManagerFactory;
import org.hibernate.ejb.Ejb3Configuration;

/**
 *
 * @author Pete
 */
public class PersistenceProvider implements Provider<EntityManagerFactory> {

    private DataSource dataSource;

    @Inject
    public PersistenceProvider(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public EntityManagerFactory get() {
        Ejb3Configuration cfg = new Ejb3Configuration();
        
        cfg.addResource("META-INF/persistence.xml"); //add an hbm.xml file
        cfg.setDataSource(dataSource); // set data source

        return cfg.buildEntityManagerFactory();
    }
}
